package com.iflysse.controller;

import com.iflysse.common.ResultEnum;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.AddChapterBoReq;
import com.iflysse.pojo.bo.req.ChapterIdBoReq;
import com.iflysse.pojo.bo.req.CourseIdBoReq;
import com.iflysse.pojo.bo.resp.ChapterBoResp;
import com.iflysse.pojo.bo.resp.ChapterListBoResp;
import com.iflysse.pojo.vo.common.CommonVoResp;
import com.iflysse.pojo.vo.req.AddChapterVoReq;
import com.iflysse.pojo.vo.req.ChapterIdVoReq;
import com.iflysse.pojo.vo.req.ChapterVoReq;
import com.iflysse.pojo.vo.resp.ChapterResp;
import com.iflysse.pojo.vo.resp.ChapterVoResp;
import com.iflysse.services.ChapterService;
import com.iflysse.util.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/chapter")
@Api(value = "老师端章的接口")
@Log4j
public class ChapterController {


    @Autowired
    private ChapterService chapterService;



    /**
     * 删除章
     */
    @PostMapping("/deleteChapter")
    @ApiOperation("老师端删除章接口")
    public CommonVoResp deleteChapter(ChapterIdVoReq chapterIdVoReq){

        CommonVoResp commonVoResp=new CommonVoResp();
        try{

            log.info("老师端删除章接口参数bean:"+chapterIdVoReq);
            ChapterIdBoReq chapterIdBoReq= CommonUtil.objToObj(chapterIdVoReq, ChapterIdBoReq.class);
            CommonBoResp commonBoResp= chapterService.deleteChapterByChapterId(chapterIdBoReq);
            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);

            log.info("老师端删除章接口响应bean:"+commonVoResp);
            return commonVoResp;
        }catch (Exception e){
            log.error("老师端删除章接口出现异常："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }

    }




    @PostMapping("/addChapter")
    @ApiOperation("老师端创建章接口")
    public CommonVoResp addChapter(AddChapterVoReq addChapterVoReq){

        CommonVoResp commonVoResp=new CommonVoResp();
        try{

            log.info("老师端创建章的参数bean："+addChapterVoReq);
            AddChapterBoReq addChapterBoReq= CommonUtil.objToObj(addChapterVoReq, AddChapterBoReq.class);
            log.info("老师端调用创建章的业务接口参数bean:"+addChapterBoReq);
            CommonBoResp commonBoResp= chapterService.addChapter(addChapterBoReq);
            log.info("老师端调用创建章的业务接口响应bean:"+commonBoResp);
            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);
            log.info("老师端创建章的响应bean:"+commonVoResp);
            return commonVoResp;
        }catch (Exception e){
            log.error("老师端创建章出现异常，信息为："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }
    }


    @PostMapping("/queryAllChapter")
    @ApiOperation("根据课程编号查询章列表接口")
    public ChapterVoResp queryAllChapter(ChapterVoReq chapterVoReq){

        ChapterVoResp chapterVoResp=new ChapterVoResp();
        try{
            log.info("老师端根据课程编号查询章得信息为："+chapterVoReq);
            CourseIdBoReq courseIdBoReq= CommonUtil.objToObj(chapterVoReq, CourseIdBoReq.class);
            ChapterListBoResp chapterListBoResp= chapterService.loadChapterByCourseId(courseIdBoReq);
            List<ChapterResp> result=new ArrayList<ChapterResp>();
            List<ChapterBoResp> chapters= chapterListBoResp.getChapterBoRespList();
            chapters.forEach(chapterBoResp -> {
                ChapterResp chapterResp=new ChapterResp();
                chapterResp.setChapterId(chapterBoResp.getChapterId());//章得编号
                chapterResp.setChapterName(chapterBoResp.getChapterTitle());//章得标题
                chapterResp.setStudyLength(chapterBoResp.getLearningTime());//学习时长
                result.add(chapterResp);
            });
            chapterVoResp.setResult(result);
            chapterVoResp.setCode(ResultEnum.SUCCESS.getCode());
            chapterVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("教师端根据课程编号查询章得信息列表响应信息为："+chapterVoResp);

            return chapterVoResp;
        }catch (Exception e){
            log.error("查询课程下得章列表出现异常："+e);
            CommonUtil.printError(chapterVoResp);
            return chapterVoResp;
        }
    }


}
